home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
modprolg
/
mod-prol.lha
/
Prolog
/
modlib
/
src
/
$modules.P
< prev
next >
Wrap
Text File
|
1992-05-30
|
7KB
|
202 lines
/****************************************************************************
* *
* File : $MODULES.P *
* Created : 10/12/1991 *
* *
****************************************************************************/
/****************************************************************************
* *
* This file has been changed by to include Modules Extensions *
* Changes by : Brian Paxton 1991/92 *
* Last update : June 1992 *
* *
* Organisation : University of Edinburgh. *
* For : Departments of Computer Science and Artificial Intelligence *
* Fourth Year Project. *
* *
****************************************************************************/
/* This file is loaded when the system is booted.
This file must define the predicate $setup_modules/0
*/
/* For more information on how the modules environment is represented in
the database, see file $consult.P */
$modules_export([$setup_modules/0,$dismantle_name/3,
$check_destination/2,$move_clause/4,$isa_structuretag/1,
$current_structure/2,$structure/3,$structure/4,
$check_not_function/1]).
% setup_modules/0 is called when the system is first booted.
$setup_modules :-
gennum(_), % Just to ensure that the number 0 is never generated,
% as 0 is used for the root structure tag.
$writename('Modules Extension Version 1.0, June 1992'), $nl,
$assert(($module_functor(_,_,_,_,_,_,_,_,_,_) :- fail)),
$assert(($module_signature(_,_,_,_,_) :- fail)),
$assert($module_structure(root,0,[],[],[])).
% $dismantle_name/3
%
% Dismantles a tagged term into its constituents.
$dismantle_name(Internal, Name, Tag) :-
nonvar(Internal),
$atom(Internal),
$name0(Internal, Chars),
$append(Tag0, [0'_,0'_|Name0], Chars), !,
$name_2(Name, Name0),
$name(Tag, Tag0).
$dismantle_name(Internal, Name, Tag) :-
var(Internal),
$atom(Name),
$integer(Tag),
$name0(Name, Name0),
$name(Tag, Tag0),
$append(Tag0, [0'_,0'_|Name0], Chars), !,
$name_2(Internal,Chars).
$dismantle_name(Internal, Name, perv) :-
( $atom(Internal) ; $atom(Name) ),
Internal = Name.
% $check_destination(+Clause, -Tag)
%
% Find the structure to which Clause belongs.
% Used by $assert.P and $retr.P
$check_destination((Head :- _), Oldtag) :-
$check_destination(Head, Oldtag),!.
$check_destination(Head, Oldtag) :-
$pervasive0(Head) -> Oldtag = perv ;
( $functor0(Head,Name),
$dismantle_name(Name,_,Oldtag) ).
% $move_clause(+Oldterm, +Oldtag, +Newtag, -Newterm)
%
% The tags of term Oldterm are replaced by Newtag to give Newterm. If any of
% Oldterm's existing tags are not equal to Oldtag, then an error is
% written and the call fails.
% Called $move_clause as it is used by $assert.P, $retr.P and $call.P
% to move clauses to remote structures.
$move_clause(Term,Tag,Tag,Term) :- !.
$move_clause(Term,_,_,Term) :-
( var(Term) ; number(Term) ; $is_buffer(Term) ),!.
$move_clause([],_,_,[]) :- !.
$move_clause([Head|Tail],Old,New,[Nhead|Ntail]) :- !,
$move_clause(Head,Old,New,Nhead),
$move_clause(Tail,Old,New,Ntail).
$move_clause(':'(_,_),_,_,_) :- !,
$writename('*** Error: Cannot assert/retract/call a clause with non-dereferenced paths'),
$nl, fail.
$move_clause(Term,Old,New,NTerm) :-
( $structure(Term) ; $atom(Term) ),
$univ(Term,[Name|Args]),
( $pervasive0(Term) -> Newname = Name ;
( ( $dismantle_name(Name, _, Old),
$arity(Term, Arity),
$symtype($mapped_function(_,_,_,_),Type),
( ( Type > 0,
$mapped_function(Textual,Arity,Name,Old) ) ;
Textual = Name ), !,
$dismantle_name(Textual,Part,_),
$dismantle_name(Newname0,Part,New),
( ( Type > 0,
$mapped_function(Newname0,Arity,Newname,_) ) ;
Newname = Newname0 ) ) ;
( $writename('*** Error: Cannot move clause - contains references to substructures'),
$nl,fail) ) ), !,
$move_clause(Args,Old,New,Nargs), !,
$univ(NTerm,[Newname|Nargs]).
% $isa_structuretag(Tag)
%
% Checks to see if argument is a structure tag.
$isa_structuretag(X) :-
nonvar(X),
( X == perv ;
( $symtype($module_structure(_,_,_,_,_), Type),
Type > 0,
( $module_structure(_,X,_,_,_) ;
( $module_structure(_,_,Subs,_,_),
$memberchk(_ ---> X, Subs) ) ) ) ), !.
% current_structure(-Strtag)
%
% The user uses a call current_structure/1 which is
% converted by fun_rel/3 into $current_structure(X,<structure tag>).
% Therefore, definition of this predicate is trivial. */
$current_structure(X,X).
% $structure(Ref, Structure, CurrentStr)
% $structure(Ref, Structure, Withrespectto, CurrentStr)
%
% Return tag of a given names structure (with respect to the third argument
% structure).
$structure(Ref, Structure, Current) :-
$structure(Ref, Structure, 0, Current).
$structure(0, root, 0, Current) :- !.
$structure(Tag, Name, WRT, Cur) :-
$get_structure(WRT,_,Wsubs,_,_),
$dismantle_name(Name,Name0,Ntag),
( ( Ntag == Cur ; Ntag == perv ) ->
$memberchk(Name0 ---> Tag, Wsubs) ;
( $get_structure(Cur,_,Csubs,_,_),
$member(Substr ---> Ntag, Csubs),
$prefix_path(Substr,Search,Name0),
$memberchk(Search ---> Tag, Wsubs) ) ),!.
% $get_structure(Tag, Name, Substrs, Preds, Funs)
%
% Given a structure tag, this routine will the return the name and signature
% of that structure. Will build signatures for non-top-level structures too.
% Structures may not have a unique name, so this routine just returns the
% first it finds.
$get_structure(Tag,Name,S,P,F) :-
$module_structure(Name,Tag,S,P,F), !.
$get_structure(Tag,Fullname,S,P,F) :-
$module_structure(Name,_,S0,P0,F0),
$memberchk(Str ---> Tag, S0),
$prefix_path(Str,Search,X),
$setof(X ---> Y, X^Y^$member(Search ---> Y,S0), S),
$setof(X/N ---> Y, X^Y^N^$member(Search/N ---> Y,P0), P),
$setof(X/N ---> Y, X^Y^N^$member(Search/N ---> Y,F0), F),
( Name == root -> Fullname = Str ;
Fullname = Name:Str ), !.
$prefix_path(First:Path, First:Search, X) :- !,
$prefix_path(Path, Search, X).
$prefix_path(Path, Path:X, X).
% $check_not_function(Clause)
%
% Used by the assert and retract family of predicates to check that their
% argument has not been declared as a function.
$check_not_function(Clause) :-
$symtype($declared_function(_), Type),
Type > 0, !,
( Clause = (Head :- _) -> true ;
Clause = Head ),
( $declared_function(Head) ->
( $functor(Head, Name, Arity),
$writename('*** Error : Cannot assert '),
$writename(Name), $writename('/'), $writename(Arity),
$writename(' - it has been declared as a function'), $nl,
fail ) ;
true ), !.
$check_not_function(_).